package com.ww.word.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ww.core.activity.SlideMenuActivity;
import com.ww.word.R;
import com.ww.word.adapter.StudyAdapter;
import com.ww.word.db.StudyDbHelper;
import com.ww.word.entity.Study;
import com.ww.word.entity.Word;
import com.ww.word.ui.menu.LeftFragment;

/**
 * 复习页面 
 * @version 2013-12-5
 * @author wangwei
 */
public class ReviewActivity extends SlideMenuActivity {
	private List<Word> list = new ArrayList<Word>();
	private static ViewPager pager;
	private int index = 0;
	private TextView num;
	private View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_review, container, false);
		initView(view);
		return view;
	}

	private void initView(View view) {
		setTitle(view, LeftFragment.mTitleName);
		setTitle(view, LeftFragment.mTitleName);
		pager = (ViewPager) view.findViewById(R.id.pager);
		num = (TextView) view.findViewById(R.id.num);

		showProgressDialog(context);
		new Thread(new Runnable() {
			@Override
			public void run() {
				getData();
				handler.sendEmptyMessage(1);
			}
		}).start();
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (list.size() > 0) {
				Word w = new Word();
				list.add(w);
				pager.setAdapter(new StudyAdapter(context, list));
				pager.setOnPageChangeListener(new GuidePageChangeListener());
				pager.setCurrentItem(index);
				num.setText((index + 1) + "/" + list.size());
				num.setVisibility(View.VISIBLE);
				closeFailView(view);
			} else {
				num.setVisibility(View.GONE);
				failView(view, getString(R.string.review_empty), R.drawable.list_empty_icon);
			}
			hideProgressDialog();
		}
	};

	private void getData() {
		StudyDbHelper h = new StudyDbHelper(context);
		Study s = h.getReviewList(context.getIntent().getStringExtra("time"));
		if (s != null) {
			list = s.getList();
		}
	}

	public static void ddd() {
		pager.setCurrentItem(0);
	}

	private final class GuidePageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			num.setText((arg0 + 1) + "/" + list.size());
		}
	}

}
